# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=yash
pkgver=2.57
pkgrel=0
pkgdesc="Yet another shell"
url="https://yash.osdn.jp"
arch="all"
license="GPL-2.0-or-later"
makedepends="ncurses-dev"
checkdepends="ed"
install="
	$pkgname.post-install
	$pkgname.pre-deinstall
	$pkgname.post-upgrade
	"
subpackages="$pkgname-doc $pkgname-binsh::noarch"
source="https://github.com/magicant/yash/releases/download/$pkgver/yash-$pkgver.tar.xz
	default-yashrc.patch
	sample-yashrc.patch
	tests-musl-regex.patch
	tests-busybox-ps.patch
	tests-busybox-argv0.patch
	global-yashrc
	"

_compdir="usr/share/yash/completion"

# Move some bigger completion files to separate subpackages.
_comps="git:git:git[gkx]:git-*
	openssh-client:ssh:ssh-*
	subversion:svn
	valgrind:valgrind
	"
for _i in $_comps; do
	subpackages="$subpackages ${_i%%:*}-yash-completion:_completion:noarch"
done
subpackages="$subpackages $pkgname-completion::noarch"

prepare() {
	# setgid bit is present on the riscv64 builder, causing 58 tests to fail
	find . -type d -exec chmod -c g-s {} \;

	default_prepare

	# Remove completions for programs that are not available on Alpine
	# (just to decrease size of the package).
	cd share/completion
	rm carthage dnf pgawk useradd
}

build() {
	sh ./configure \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--disable-nls
	make
}

check() {
	make tests LANG=en_US.UTF-8 || {
		cat tests/summary.log
		return 1
	}
}

package() {
	make DESTDIR="$pkgdir" install
	rm -Rf "$pkgdir"/usr/share/man/ja

	install -D -m644 "$srcdir"/global-yashrc "$pkgdir"/etc/yashrc
}

_completion() {
	local name="${subpkgname%-yash-completion}"
	pkgdesc="Yash completions for $name"
	depends="$pkgname-completion"
	install_if="$pkgname-completion=$pkgver-r$pkgrel $name"

	local files=$(printf '%s\n' $_comps \
		| sed -En "s|^$name:(.*)|\1|p" \
		| tr : ' ')
	[ "$files" ] || die "$name not found in \$_comps"

	local f; for f in $files; do
		amove usr/share/yash/completion/$f
	done
}

completion() {
	pkgdesc="$pkgdesc (command-line completion)"
	depends=""

	amove usr/share/yash/completion
}

binsh() {
	pkgdesc="yash as /bin/sh"
	provides="/bin/sh"
	provider_priority=50  # lower (other provider is busybox-binsh)

	mkdir -p "$subpkgdir"/bin
	ln -s /usr/bin/yash "$subpkgdir"/bin/sh
}

sha512sums="
6604ce54f698740d99e4ce3e6ff93b10936353b0690871b4dd1a0ef4569fc228284f551398b4912d324c8627eccec4cd51ac58508f741eb47b315560f02bff01  yash-2.57.tar.xz
8fe512d219204ddbbd59d683732c930970db08a23997c88e91035b60fd5141ea813c4d3ab2d7f19fcf6147fca59b085600a904de0c595dd92f24ab63e19ae91d  default-yashrc.patch
ac24612a82a353d65e2e1a6b99727363be62eb7801eae3109c4661d694d82ddd5c9fc1233443e69d8b26abab725eeaac2c4110e26b1b366983b13cbf7699ebf5  sample-yashrc.patch
441f8eab939c524edf88c164a0d44ea76faa892ba8c67924ca93bb266d0b65885b498b3e8df9ea723ef8263fce9cd9bd0ffb346b8b7b96c84c91f8f141842cf3  tests-musl-regex.patch
beac976835806cce7ec1b89672d297a88ab325b5cbd49033604f620f40025c42f4bcb2c2a8a5930e7df48d5ef709a8a0d79d03eb1836dd1052e9500bd288de74  tests-busybox-ps.patch
3ec0d1ca964fed8a4afcc8c5db0126f8e952e08ea9638f41565478dbc6d74623c6d88e503cd30b27fc7b58ce00fff88f45615d05468e9b9eb0db379695c61cb9  tests-busybox-argv0.patch
778b6a4fa415692293cc9a91074be373572106dc90aeb7490b37643a737f223861f05a7245e4889d8ffea975570f56fd746877726630b9486c481d3159a23e30  global-yashrc
"
